package com.example.demovote.mapper;

import com.example.demovote.entity.Items;
import com.example.demovote.entity.Vote;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 投票系统-题目表-2 Mapper 接口
 * </p>
 *
 * @author ly
 * @since 2024-03-18
 */
public interface VoteMapper extends BaseMapper<Vote> {

    @Select("select a.*,b.icnt,c.ucnt from tp_vote a \n" +
            "	join (select vid, count(*) icnt from tp_items group by vid) b on a.id=b.vid\n" +
            "	left join(select vid,count(usid) ucnt from tp_record group by vid) c on a.id=c.vid;")
    List<Vote> select();

    @Select("select a.*,b.icnt,c.ucnt from tp_vote a \n" +
            "	join (select vid, count(*) icnt from tp_items group by vid) b on a.id=b.vid\n" +
            "	left join(select vid,count(usid) ucnt from tp_record group by vid) c on a.id=c.vid" +
            " where a.id=#{id}")
    @Results(
            value={
                    @Result(column = "id", property = "id"),
                    @Result(column = "id", property = "items", many = @Many(
                            select = "selectItemsByVid"
                    ))
            }
    )
    Vote selectById(int id);

    @Select("select a.*, ifnull(b.cnt,0) cnt\n" +
            "  from tp_items a\n" +
            "  left join (select iid, count(*) cnt from tp_record where vid=#{vid} group by iid) b on a.id=b.iid\n" +
            " where vid=#{vid};")
    List<Items> selectItemsByVid(int vid);




}
